﻿using System;
using UnityEditor;
using UnityEngine;

namespace EditorFrameWork
{
    public class SplitView : GUIBase
    {
        public RectExtension.SplitType SplitType = RectExtension.SplitType.Vertical;
        public event Action<Rect> FirstArea, SecondArea;
        public event Action OnBeginResize, OnEndResize;

        public float SplitSize = 200;
        public float MinSize = 100;
        private bool mResizeing;

        public bool Dragging
        {
            get { return mResizeing; }
            set
            {
                if (mResizeing != value)
                {
                    mResizeing = value;
                    if (value)
                    {
                        OnBeginResize?.Invoke();
                    }
                    else
                    {
                        OnEndResize?.Invoke();
                    }
                }
            }
        }

        public override void OnGUI(Rect position)
        {
            base.OnGUI(position);
            var rects = position.Split(SplitType, SplitSize, 4);
            var mid = position.SplitRect(SplitType, SplitSize, 4);
            //左边区域
            {
                FirstArea?.Invoke(rects[0]);
                GUI.Box(rects[0], "First");
            }
            //右边区域
            {
                SecondArea?.Invoke(rects[1]);
                GUI.Box(rects[1], "Second");
            }
            EditorGUI.DrawRect(mid.Zoom(RectExtension.AnchorType.MiddleCenter, -2f), Color.gray);
            var e = Event.current;
            if (mid.Contains(e.mousePosition))
            {
                if (SplitType == RectExtension.SplitType.Vertical)
                {
                    EditorGUIUtility.AddCursorRect(mid, MouseCursor.ResizeHorizontal);
  
                }
                else
                {
                    EditorGUIUtility.AddCursorRect(mid, MouseCursor.ResizeVertical);
                }
            }

            switch (e.type)
            {
                case EventType.MouseDown:
                    if (mid.Contains(e.mousePosition))
                    {
                        Dragging = true;
                    }

                    break;
                case EventType.MouseDrag:
                    if (Dragging)
                    {
                        if (SplitType == RectExtension.SplitType.Vertical)
                        {
                            SplitSize += e.delta.x;
                            SplitSize = Mathf.Clamp(SplitSize, MinSize, position.width - MinSize);
                        }
                        else
                        {
                            SplitSize += e.delta.y;
                            SplitSize = Mathf.Clamp(SplitSize, MinSize, position.height - MinSize);
                        }

                        e.Use();
                    }

                    break;
                case EventType.MouseUp:
                    if (Dragging)
                    {
                        Dragging = false;
                    }

                    break;
            }
        }

        protected override void OnDispose()
        {
            FirstArea = null;
            SecondArea = null;
            OnBeginResize = null;
            OnEndResize = null;
        }
    }
}